// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.meta;

import java.util.Vector;

import tc.crase.db.Xattr;
import tc.crase.db.XattrLNK;
import tc.crase.sys.XobjectEx;

public class DescrItemPath extends DescrItem {
	public Vector<String> path = new Vector<String>();
	
	public DescrItemPath() {
	}
	
	public DescrItemPath(String p0) {
		add(p0);
	}
	
	public DescrItemPath(String p0, String p1) {
		add(p0);
		add(p1);
	}

	public DescrItemPath(String p0, String p1, String p2) {
		add(p0);
		add(p1);
		add(p2);
	}
	
	public void add(String item) {
		path.add(new String(item));
	}
	
	public String getValue(XobjectEx xobj, Vector<Xattr> used) {
		if (path.size() == 0) return "";
		return getValue(xobj, 0, used);
	};
				
	public String getValue(XobjectEx xobj, int index, Vector<Xattr> used) {
		Xattr result = null;
		String name = path.get(index);
		
		for (Xattr attr : xobj) {
			if (name.equals(attr.getName()) == false) continue;
			if (used.contains(attr)) continue;
			result = attr;
			break;
		}
		
		if (result == null) return "";
		
		if (++index >= path.size()) { 
			used.add(result);
			return result.getValue();
		}
		
		XattrLNK lnk = result.getAsLNK();
		
		if (lnk == null) return "";
		
		used.add(result);
		return getValue((XobjectEx)(lnk.getOpposite().getParent()), index, used);
	}
}
